package com.itheima.reggie.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    //套餐分页查询
    @GetMapping("/setmeal/page")
    public ResultInfo findByPage(
            String name,
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize
    ) {//1.接收参数
        //2.调用service查询
        Page<Setmeal> page = setmealService.findByPage(pageNum, pageSize, name);
        //3.返回结果
        return ResultInfo.success(page);

    }
    //套餐新增
    @PostMapping("/setmeal")
    public ResultInfo save(@RequestBody Setmeal setmeal) {
        //1.接收参数
        //2.调用service
        setmealService.save(setmeal);
        //3.返回结果
        return ResultInfo.success(null);
    }

    //套餐删除
    @DeleteMapping("/setmeal")
    public ResultInfo deleteBatchIds(@RequestParam List<Long> ids) {
        //1.接收参数
        //2.调用service删除
        if (CollectionUtil.isNotEmpty(ids)) {//非空判断
            setmealService.deleteBatchIds(ids);
        }
        //3.返回结果
        return ResultInfo.success(null);
    }
//套餐起售停售
    @PostMapping("/setmeal/status/{status}")
    public ResultInfo updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids) {
        setmealService.updateStatus(status, ids);
        return ResultInfo.success(null);

    }

    //套餐回显
    @GetMapping("/setmeal/{id}")
    public ResultInfo findById(@PathVariable Long id) {
        //1.接收套餐id
        //2.调用service查询
        Setmeal setmeal = setmealService.findById(id);
        //3.返回结果
        return ResultInfo.success(setmeal);
    }

    //套餐修改
    @PutMapping("/setmeal")
    public ResultInfo update(@RequestBody Setmeal setmeal) {
        //1.接收请求参数
        //调用service修改
        setmealService.update(setmeal);
        //3.返回消息
        return ResultInfo.success(null);
    }
}
